package ia;

import book.Booklist;
import book.Book;
import java.util.Scanner;

public class Deletebook implements IA{
    public void work(Booklist booklist){
        System.out.println("删除图书.........");

        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你想要删除的书名");
        String bookname = sc.nextLine();

        int cutsize = booklist.getUsesize();
        int del = -1;
        for (int i = 0; i < cutsize; i++) {
            Book book = booklist.getBooks(i);
            if (book.getTitle().equals(bookname)) {
               del = i;
               break;
                }
        }
        if (del == -1){
            System.out.println("没有找到你想要删除的书");
            return;
        }
        for (int j = del; j < cutsize - 1; j++) {
            Book book1 = booklist.getBooks(j + 1);
            booklist.setBooks(j, book1);
        }
        // 将最后一个位置置为 null
        booklist.setBooks(cutsize - 1, null);
        // 调整有效元素数量
        booklist.setUsesize(cutsize - 1);
        System.out.println("已经成功删除");
    }
}
